<template>
  <div class="sub-filter" v-if="filterData && !filterLoading">
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a href="javascript:;" @click="changeBrand(brand.id)" :class="{ active: brand.id ===  filterData.selectedBrand }"
          v-for="brand in filterData['brands']" :key="brand.id">{{ brand.name }}</a>
      </div>
    </div>
    <div class="item" v-for="item in filterData['saleProperties']" :key="item.id">
      <div class="head">{{ item.name }}：</div>
      <div class="body">
        <a href="javascript:;" @click="changeProp(item, property.id)"
          :class="{ active: property.id === item.selectedAttr }" v-for="property in item.properties"
          :key="property.id">{{ property.name }}</a>
      </div>
    </div>
  </div>
  <div v-else class="sub-filter">
    <XtxSkeleton class="item" width="800px" height="40px" animated />
    <XtxSkeleton class="item" width="800px" height="40px" animated />
    <XtxSkeleton class="item" width="600px" height="40px" animated />
    <XtxSkeleton class="item" width="600px" height="40px" animated />
    <XtxSkeleton class="item" width="600px" height="40px" animated />
  </div>
</template>
<script>
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { findFilterAPI } from '@/api/category'

export default {
  name: 'SubFilter',

  setup (props, context) {
    const route = useRoute()
    const filterData = ref({})
    const filterLoading = ref(false)

    watch(() => route.params.id, (newSubId) => {
      if (newSubId && `/category/sub/${newSubId}` === route.path) {
        filterLoading.value = true
        findFilterAPI(newSubId).then(data => {
          data.result.selectedBrand = null
          data.result.brands.unshift({ id: null, name: '全部' })
          data.result.saleProperties.forEach(item => {
            item.selectedAttr = null
            item.properties.unshift({ id: null, name: '全部' })
          })
          filterData.value = data.result
          filterLoading.value = false
        })
      }
    }, { immediate: true })

    const getFilterParams = () => {
      const obj = {
        brandId: '',
        attrs: []
      }

      obj.brandId = filterData.value.selectedBrand
      filterData.value.saleProperties.forEach(item => {
        if (item.selectedAttr) {
          const prop = item.properties.find(prop => prop.id === item.selectedAttr)
          obj.attrs.push({ groupName: item.name, propertyName: prop.name })
        }
      })
      return obj
    }

    function changeBrand (brandId) {
      if (filterData.value.selectedBrand === brandId) {
        return
      }
      filterData.value.selectedBrand = brandId
      context.emit('filter-change', getFilterParams())
    }

    function changeProp (item, propId) {
      if (item.selectedAttr === propId) {
        return
      }
      item.selectedAttr = propId
      context.emit('filter-change', getFilterParams())
    }

    return {
      filterData, filterLoading, changeBrand, changeProp
    }
  }
}
</script>
<style scoped lang='less'>
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;

  .item {
    display: flex;
    line-height: 40px;

    .head {
      width: 80px;
      color: #999;
    }

    .body {
      flex: 1;

      a {
        margin-right: 36px;
        transition: all .3s;
        display: inline-block;

        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
}

.xtx-skeleton {
  padding: 10px 0;
}
</style>
